/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

import com.mol.market.R;
import com.mol.market.common.network.ApiAsyncTask;

/**
 * @author Chaos	
 * @Description TODO
 * @date 2013-1-21 
 */
public abstract class CommonFragment extends BaseFragmentWrapper
                implements ApiAsyncTask.ApiRequestListener,OnClickListener{

    private View mLoading = null;
    private View mProgressbar = null;
    private View mRetry = null;
    
    private View mCustomView = null;
    
    @Override
    protected View onCreateViewPort(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        FrameLayout root = (FrameLayout) inflater.inflate(R.layout.common_fragment, null);
        setup(root);
        setState(State.LOADING);
        return root;
    }

    abstract protected View initLayout();
    
    abstract protected void doLoad();
    
    @Override
    public void onStart() {
        super.onStart();
        
        doLoad();
    }
    
    @Override
    protected void dealloc() {
        super.dealloc();
        this.mLoading = null;
        this.mRetry   = null;
        this.mProgressbar = null;
        this.mCustomView = null;
    }
    
    public void setState(State state){
        switch (state) {
        case LOADING:
            mLoading.setVisibility(View.VISIBLE);
            mProgressbar.setVisibility(View.VISIBLE);
            mRetry.setVisibility(View.GONE);
            mCustomView.setVisibility(View.GONE);
            break;
        case SUCCESS:
            mLoading.setVisibility(View.GONE);
            mProgressbar.setVisibility(View.GONE);
            mRetry.setVisibility(View.GONE);
            mCustomView.setVisibility(View.VISIBLE);
            break;
        case ERROR:
            mLoading.setVisibility(View.VISIBLE);
            mProgressbar.setVisibility(View.GONE);
            mRetry.setVisibility(View.VISIBLE);
            mCustomView.setVisibility(View.GONE);
            break;
        }
    }
    
    private void setup(FrameLayout root){
        mLoading     = root.findViewById(R.id.loading);
        mProgressbar = root.findViewById(R.id.progressbar);
        mRetry       = root.findViewById(R.id.no_data); 
        
        //setup custom view
        mCustomView = initLayout();
        
        //check null
        if(mCustomView == null){
            return ;
        }
        
        //check layout
        if(mCustomView.getLayoutParams() == null){
            root.addView(mCustomView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }else{
            root.addView(mCustomView);
        }
    }
    
    @Override
    public void onSuccess(int method, Object obj) {
        setState(State.SUCCESS);
    }

    @Override
    public void onError(int method, int statusCode) {
        setState(State.ERROR);
    }
    
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.no_data){
            setState(State.LOADING);
            doLoad();
        }
    }
    
    enum State{
        LOADING,
        SUCCESS,
        ERROR
    }

}
